package com.u.social.sale.controller;


import com.u.social.sale.entity.Order;
import com.u.social.sale.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alex Wu
 * @since 2025-03-10
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/cart")
    public ResponseEntity<Boolean> createOrderByCart() {
        Boolean isSuccess = orderService.addOrderByCart(123L);
        return ResponseEntity.status(HttpStatus.OK).body(isSuccess);
    }

    @PostMapping("/direct")
    public ResponseEntity<Boolean> createOrderByProduct(@RequestParam("productId") Long productId,@RequestParam("skuId") Long skuId) {
        Boolean isSuccess = orderService.addOrderByProduct(productId, skuId);
        return ResponseEntity.status(HttpStatus.OK).body(isSuccess);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Order> getOrderById(@PathVariable Long id) {
        Order order = orderService.getById(id);
        if (order != null) {
            return ResponseEntity.ok(order);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<Order> updateOrder(@PathVariable Long id, @RequestBody Order order) {
        order.setId(id);
        if (orderService.updateById(order)) {
            return ResponseEntity.ok(order);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteOrder(@PathVariable Long id) {
        if (orderService.removeById(id)) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
    }

}
